// pages/add/rent/rent.js
Page({
  data: {
    isEdit: false,
    id: "",
    bottomHeight: getApp().globalData.bottomHeight,
    fileList: [],
    imgIds: [],
    title: "",
    content: "",
    rentType: "1",
    rental: "",
    parkingFloor: "1",
    parkingType: "1",
    houseFloor: "",
    payMethod: "1",
    houseArea: "",
    houseType: "",
    rentMethod: "1",
    isSync: true,
    btn_status: true
  },
  onLoad: function (options) {
    if (options.edit == 1) {
      this.setData({
        isEdit: true,
        id: options.id
      });
      this.getDetail();
    }
  },
  getDetail() {
    getApp().globalData.http.detail({
      id: this.data.id
    }).then(res => {
      let tmp = res.data;
      let imgs = [];
      tmp.imgUrls = tmp.imgUrls.split(",");
      tmp.imgUrls.forEach(item => {
        imgs.push({
          url: item
        });
      });
      this.setData({
        fileList: imgs,
        imgIds: tmp.imgIds.split(","),
        title: tmp.title,
        content: tmp.content,
        rentType: tmp.rentVo.rentType,
        rental: tmp.rentVo.rental,
        parkingFloor: tmp.rentVo.parkingFloor,
        parkingType: tmp.rentVo.parkingType,
        houseFloor: tmp.rentVo.houseFloor,
        payMethod: tmp.rentVo.payMethod,
        houseArea: tmp.rentVo.houseArea,
        houseType: tmp.rentVo.houseType,
        rentMethod: tmp.rentVo.rentMethod,
        isSync: tmp.isSync == 1 ? true : false
      });
    });
  },
  upload(e) {
    getApp().globalData.util.imgsUpload(this, e, this.data.fileList, this.data.imgIds);
  },
  delete(e) {
    let i = e.detail.index;
    let fileList = this.data.fileList;
    let imgIds = this.data.imgIds;
    fileList.splice(i, 1);
    imgIds.splice(i, 1);
    this.setData({
      fileList: fileList,
      imgIds: imgIds
    });
  },
  syncHandle() {
    this.setData({
      isSync: !this.data.isSync
    });
  },
  typeSel(e) {
    this.setData({
      rentType: e.currentTarget.dataset.type
    });
  },
  posHandle(e) {
    this.setData({
      parkingFloor: e.currentTarget.dataset.floor
    });
  },
  parkingTypeHandle(e) {
    this.setData({
      parkingType: e.currentTarget.dataset.type
    });
  },
  payHandle(e) {
    this.setData({
      payMethod: e.currentTarget.dataset.pay
    });
  },
  rentHandle(e) {
    this.setData({
      rentMethod: e.currentTarget.dataset.type
    });
  },
  submit() {
    let params = {
      type: 3,
      title: this.data.title,
      content: this.data.content,
      imgIds: this.data.imgIds.join(","),
      isSync: this.data.isSync ? 1 : 0,
      rentVoStr: {
        rentType: this.data.rentType,
        rental: this.data.rental,
        parkingFloor: this.data.parkingFloor,
        parkingType: this.data.parkingType,
        houseFloor: this.data.houseFloor,
        houseType: this.data.houseType,
        houseArea: this.data.houseArea,
        rentMethod: this.data.rentMethod,
        payMethod: this.data.payMethod,
      }
    };
    if (this.data.btn_status) {
      if (!params.imgIds) {
        getApp().globalData.vant.toast("您还没有上传图片");
      } else if (!params.title) {
        getApp().globalData.vant.toast("商品标题不能为空");
      } else if (!params.content) {
        getApp().globalData.vant.toast("商品介绍不能为空");
      } else if (!params.rentVoStr.rental) {
        getApp().globalData.vant.toast("租金不能为空");
      } else if (!getApp().globalData.reg.price.test(params.rentVoStr.rental)) {
        getApp().globalData.vant.toast("请输入正确的租金");
      } else if (params.rentVoStr.rental > 9999.99) {
        getApp().globalData.vant.toast("租金最大不能超过9999.99");
      } else if (!getApp().globalData.reg.price.test(params.rentVoStr.rental)) {
        getApp().globalData.vant.toast("请输入正确的租金");
      } else {
        if (params.rentVoStr.rentType == "2") {
          if (!params.rentVoStr.houseFloor) {
            getApp().globalData.vant.toast("楼层不能为空");
            return;
          } else if (!getApp().globalData.reg.number.test(params.rentVoStr.houseFloor)) {
            getApp().globalData.vant.toast("楼层必须为数字");
            return;
          } else if (!params.rentVoStr.houseArea) {
            getApp().globalData.vant.toast("面积不能为空");
            return;
          } else if (!getApp().globalData.reg.price.test(params.rentVoStr.houseArea)) {
            getApp().globalData.vant.toast("面积必须为数字");
            return;
          } else if (!params.rentVoStr.houseType) {
            getApp().globalData.vant.toast("房型不能为空");
            return;
          }
        }
        this.setData({
          btn_status: false
        });
        if (!this.data.isEdit) {
          params.rentVoStr = JSON.stringify(params.rentVoStr);
          getApp().globalData.http.squarePost(params).then(res => {
            getApp().globalData.vant.toast({
              type: "success",
              forbidClick: true,
              message: "发布成功",
              onClose: () => {
                wx.switchTab({
                  url: "/pages/home/home",
                  success: function (e) {
                    var page = getCurrentPages().pop();
                    if (page == undefined || page == null) return;
                    page.reset();
                  }
                });
              }
            });
          }).catch(e => {
            this.setData({
              btn_status: true
            });
          });
        } else {
          params.id = this.data.id
          params.rentVoStr = JSON.stringify(params.rentVoStr);
          getApp().globalData.http.squareUpdate(params).then(res => {
            getApp().globalData.vant.toast({
              type: "success",
              forbidClick: true,
              message: "保存成功",
              onClose: () => {
                wx.navigateBack({
                  success: function (e) {
                    var page = getCurrentPages().pop();
                    if (page == undefined || page == null) return;
                    page.reset();
                  }
                });
              }
            });
          }).catch(e => {
            this.setData({
              btn_status: true
            });
          });
        }
      }
    }
  }
})